package cn.kgc.mybatis.web.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 课工场
 * @date 2024/1/29
 * @description
 */
public class JSONUtil {

    private JSONUtil(){}

    public static  void writeJson(Object data, HttpServletResponse resp){
        // 设置响应的格式为  json
        resp.setContentType("application/json;charset=utf-8");
        // 获取打印流 将json格式的字符串 写出到前端
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            //  java  ->  String
            String strResult = cn.hutool.json.JSONUtil.toJsonStr(data);
            writer.write(strResult);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            // 关闭输出流   释放资源
            writer.close();
        }

    }

}
